package com.hx.learn.spring.boot.stater.auto;

import com.hx.learn.spring.boot.stater.model.Home;
import com.hx.learn.spring.boot.stater.model.Person;
import com.hx.learn.spring.boot.stater.model.PersonInfo;
import com.hx.learn.spring.boot.stater.properties.PersonalProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述：自动装配
 *
 * @author Minghua Chen
 * @date 2020-10-18 09:37
 */
@Configuration
@EnableConfigurationProperties(PersonalProperties.class)
public class PersonalAutoConfig {

    @Autowired
    private PersonalProperties personalProperties;

    @Bean
    public Home getHome() {
        Home home = personalProperties.getHome();
        if (home == null) {
            home = new Home();
        }
        return home;
    }

    @Bean
    public Person getPerson() {
        Person person = personalProperties.getPerson();
        if (person == null) {
            person = new Person();
        }
        return person;
    }

    @Bean
    public PersonInfo getPersonInfo(@Autowired Person person,@Autowired Home home) {
        PersonInfo personInfo = new PersonInfo();
        personInfo.setInfo(person.getId()+person.getName()+home.getId()+home.getName());
        return personInfo;
    }

}
